package com.cloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cloud.bean.Product;
import com.cloud.bean.Type;
import com.cloud.service.ProductService;
import com.cloud.service.TypeService;

@Controller
public class ProductController {

	@Autowired
	private ProductService productService;
	
	@Autowired
	private TypeService typeService;
	
	@RequestMapping("/index")
	public String Index(){
		return "index";
	}
	
	@RequestMapping("/toAdd")
	public String toAdd(Model model){
		List<Type> types = typeService.selectAll();
		model.addAttribute("types", types);
		return "add";
	}
	
	@RequestMapping("/add")
	public String add(Product product){
		productService.insertProduct(product,productService);
		return "redirect:index";
	}

	// 单表格查询所有
	@RequestMapping("/selectAll")
	public String selectAll(Model model) {
		List<Product> products = productService.selectAll();
		model.addAttribute("products", products);
		return "list";
	}
	
	//去更新
	@RequestMapping("/toUpdate")
	public String toUpdate(int id,Model model){
		Product product = productService.selectById(id);
		model.addAttribute("product", product);
		List<Type> types = typeService.selectAll();
		model.addAttribute("types", types);
		return "edit";
	}

	// 单表更新
	@RequestMapping("/UpdateProduct")
	public String UpdateProduct(Product product) {
		productService.updateProductById(product);
		return "redirect:selectAll";
	}

	// 单表根据id删除
	@RequestMapping("/deleteById")
	public String deleteById(int id) {
		productService.deleteById(id);
		return "redirect:selectAll";
	}
}
